package temp.casia.isiteam.zrb.hbase.client.util;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.log4j.Logger;

/**
 * @author dell
 * @createDate 2015年8月12日
 */
public class ConnectionManager {
	private static Logger logger = Logger.getLogger(ConnectionManager.class);
	private static int retryTimes = 0;
	
	/**
	 * 获取连接	
	 * @param config
	 * @return
	 */
	public static Connection getConn(Configuration config){
		Connection conn = null;
		try {
			conn = ConnectionFactory.createConnection(config);
		} catch (Exception e) {
			logger.error("Fail to init connection!");
			if(++retryTimes <= 5){
				try {
					Thread.sleep(2000);
				} catch (Exception e1) {}
				logger.warn("reconnect! times: " + retryTimes);
				conn = getConn(config); 
			}
		}
		return conn;
	}
	
	/**
	 * 关闭连接
	 * @param conn
	 */
	public static void closeConn(Connection conn){
		try {
			conn.close();
		} catch (Exception e) {}
	}

}
